<?php

namespace App\Repositories\Manage;

use App\Models\ComboSpec;
use App\Models\ComboSpecItem;

class ComboSpecRepository
{

    /**
     * 增加规格
     * @param int $product_i
     * @param $skus
     */
    public function updateMappings(int $product_id, $skus)
    {
        if (!$product_id) return retFail("参数错误");
        $specIds = ComboSpec::query()->where("product_id", $product_id)->pluck("id")->toArray();
        if (isset($specIds)) {
            $specIds_1 = get_column($skus, "id");
            settype($specIds, "array");
            settype($specIds_1, "array");
            $spec_ids = array_diff($specIds, $specIds_1 ?? []);

            if ($spec_ids) {
                ComboSpecItem::query()->whereIn("spec_id", $specIds)->update(['is_delete' => 1]);
                ComboSpec::query()->whereIn("id", $spec_ids)->delete();
            }
        }

        $mappings = [];
        foreach ($skus as $k => $v) {
            if ($v['id']) {
                ComboSpecItem::where(['spec_id' => $v['id']])->update(['is_delete' => 1]);
                $spec_id = intval($v['id']);

                ComboSpec::query()->where(['id' => $spec_id])->update([
                    'title' => $v['title'],
                    'amount' => $v['amount'],
                    'is_multi' => $v['is_multi'],
                    'deleted_at' => NULL,
                ]);
            } else {
                $spec_id = ComboSpec::create([
                    'title' => $v['title'],
                    'amount' => $v['amount'] ?? 0,
                    'is_multi' => $v['is_multi'],
                    'product_id' => $product_id
                ])->id;
            }
            if (!$spec_id) continue;

            foreach ($v['spec_items'] as $sv) {

                if ($sv['id']) {
                    ComboSpecItem::where(['id' => $sv['id']])->update([
                        'title' => $sv['title'],
                        'product_id' => $sv['product_id'],
                        'amount' => $sv['amount'] ?? 0,
                        'sn' => $sv['sn'] ?? 0,
                        'spec_id' => $spec_id,
                        'is_delete' => 2,
                    ]);
                    continue;
                }

                ComboSpecItem::create([
                    'title' => $sv['title'],
                    'product_id' => $sv['product_id'],
                    'amount' => $sv['amount'] ?? 0,
                    'sn' => $sv['sn'] ?? 0,
                    'spec_id' => $spec_id
                ]);
            }

        }

        return retSuc($mappings);
    }

    /**
     * 产品对应规格
     * @param int $product_id
     */
    public function getSpecs(int $product_id = 0)
    {
        if (!$product_id)
            return null;

        $redis_key = "productComboSpecList_" . $product_id;
        $specs = getRedisByKey($redis_key, 2);
        if ($specs)
            return $specs;

        $specs = ComboSpec::query()->where(['product_id' => $product_id])->get(['id', 'title', 'is_multi', 'amount']);
        if (!isset($specs))
            return null;

//        $shop_id = Product::query()->where("id", $product_id)->value("shop_id"); //上线去掉
        foreach ($specs as $k => &$v) {
            $v['spec_items'] = ComboSpecItem::query()
                ->where(['spec_id' => $v['id'], 'is_delete' => 2])
                ->get(['id', 'title', 'product_id', 'amount', 'sn']);
            //更新数据  - 上线去掉
            /*if (empty($v['spec_items'])) continue;
            foreach ($v['spec_items'] as $sk => $sv) {
                if ($sv['product_id'] != $product_id)
                    continue;
                $new_product_id = Product::query()->where(["title" => $sv['title'], 'shop_id' => $shop_id])
                    ->orderBy("stock", "desc")
                    ->orderBy("id", "desc")
                    ->value("id");
                if (!$new_product_id) continue;
                ComboSpecItem::query()->where('id', $sv['id'])->update([
                    'product_id' => $new_product_id,
                    'u_pid' => $product_id
                ]);
                dump($product_id . ' - ' . $new_product_id . ' - ' . $sv['id']);
            }*/
        }
        setRedisByDb($redis_key, $specs, null, 2);
        return $specs;
    }

}
